<?php

error_reporting(E_ALL);
include('include/config.inc.php');
$lightheader = FALSE;


	// Update DB 
	if ( isset($_POST["updatedb"]) && $_POST["updatedb"]=="ON" ){ 
	include('import.php');
	exit;
	}

if ($_SERVER["REQUEST_METHOD"]=="POST") 
{
	$sql  = "SELECT * FROM cdr WHERE CallId <> '-1'";
	
	if ( isset($_POST["fromnumber"]) && $_POST["fromnumber"]!="0" )
	{
		$sql .= " AND  OriginationNumber = '".$_POST["fromnumber"]."'";
	}
	
	if ( isset($_POST["fromname"]) && $_POST["fromname"]!="0" )
	{
		$sql .= " AND  OriginationName = '".$_POST["fromname"]."'";
	}
	
	if ( isset($_POST["andor"]) && $_POST["andor"]=="AND" )
	{
		$ANDOR = " AND";
	}
	else
	{
		$ANDOR = " OR";
	}

	if ( isset($_POST["tonumber"]) && $_POST["tonumber"]!="0" )
	{
		$sql .= $ANDOR . " CalledNumber = '".$_POST["tonumber"]."' ";
	}
	
	if ( isset($_POST["toname"]) && $_POST["toname"]!="0" )
	{
		$sql .= $ANDOR . " CalledName = '".$_POST["toname"]."' ";
	}
	
	if ( isset($_POST["andor2"]) && $_POST["andor2"]=="AND" )
	{
		$ANDOR2 = " AND";
	}
	else
	{
		$ANDOR2 = " OR ";
	}

	if ( isset($_POST["destnumber"]) && $_POST["destnumber"]!="0" )
	{
		$sql .= $ANDOR2 . " DestinationNumber = '".$_POST["destnumber"]."'";
	}
	
	if ( isset($_POST["destname"]) && $_POST["destname"]!="0" )
	{
		$sql .= $ANDOR2 . " DestinationName = '".$_POST["destname"]."'";
	}					

	if ( isset($_POST["conversationstate"]) && $_POST["conversationstate"]!="0" )
	{
		$sql .= " AND State = '".$_POST["conversationstate"]."'";
	}

	if ( isset($_POST["disconnectreason"]) && $_POST["disconnectreason"]!="0" )
	{
		$sql .= " AND DisconnectReason = '".$_POST["disconnectreason"]."'";
	}
	
	if ( isset($_POST["vaninternextern"]) && $_POST["vaninternextern"]!="0" )
	{
		if ( $_POST["vaninternextern"]=="1" )
		{
			$sql .= " AND OriginationDevice = ''";
		}

		// THIS IS MY DEVICE TO CONNECT EXTERNAL CALL TO/FROM
		// IF YOU HAVE DIFFERENT EXTERNAL DEVICES YOU WOULD NEED TO SET THEM UP HERE
		if ( $_POST["vaninternextern"]=="2" )
		$sql .= " AND OriginationDevice = 'Voip-Amt'";
	}
	if ( isset($_POST["naarinternextern"]) && $_POST["naarinternextern"]!="0" )
	{
		if ( $_POST["naarinternextern"]=="1" )
		{
			$sql .= " AND DestinationDevice = ''";
		}
		if ( $_POST["naarinternextern"]=="2" )
		$sql .= " AND DestinationDevice = 'Voip-Amt'";
	}
	// freie Nummer
	if ( isset($_POST["freienummer"]) && $_POST["freienummer"]!="" ){ 
		$sql  .= " AND OriginationNumber = '" . $_POST["freienummer"] ."' or CalledNumber = '" . $_POST["freienummer"] ."'";
	//echo "4Kommando = $sql";
	}	
	
	if ( isset($_POST["vandatetime"]) && $_POST["vandatetime"]!="" && isset($_POST["totdatetime"]) && $_POST["totdatetime"]!="" )
	{	
	# Datum umschreiben für Datenbank Format
	$Newdate1 = explode(" ", $_POST["vandatetime"]);
	$Date1 = $Newdate1[0]; 
	$Zeit = $Newdate1[1];
	# Datum umschreiben für Datenbank Format
	$Newdate = explode(".", $Date1);
	$Tag = $Newdate[0];
	$Monat = $Newdate[1];
	$Jahr = $Newdate[2];
	$Insertdate = "$Jahr-$Monat-$Tag $Zeit";
	
	# Datum umschreiben für Datenbank Format
	$Newdate2 = explode(" ", $_POST["totdatetime"]);
	$Date2 = $Newdate2[0]; 
	$Zeit2 = $Newdate2[1];
	# Datum umschreiben für Datenbank Format
	$Newdate3 = explode(".", $Date2);
	$Tag2 = $Newdate3[0];
	$Monat2 = $Newdate3[1];
	$Jahr2 = $Newdate3[2];
	$Insertdate2 = "$Jahr2-$Monat2-$Tag2 $Zeit2";
		
		$sql .= " AND  Startdate > '$Insertdate' and Startdate < '$Insertdate2'";
		#echo "1Kommando = $sql";
	}
	if ( isset($_POST["vandatetime"]) && $_POST["vandatetime"]!="" && isset($_POST["totdatetime"]) && $_POST["totdatetime"]=="" )
	{	
	# Datum umschreiben für Datenbank Format
	$Newdate1 = explode(" ", $_POST["vandatetime"]);
	$Date1 = $Newdate1[0]; 
	# Datum umschreiben für Datenbank Format
	$Newdate = explode(".", $Date1);
	$Tag = $Newdate[0];
	$Monat = $Newdate[1];
	$Jahr = $Newdate[2];
	$Insertdate = "$Jahr-$Monat-$Tag";
			
		$sql .= " AND StartDate >= '$Insertdate 00:00:00' AND StartDate <= '$Insertdate 23:59:00'";
		#echo "2Kommando = $sql";
	}
	if ( isset($_POST["vandatetime"]) && $_POST["vandatetime"]=="" && isset($_POST["totdatetime"]) && $_POST["totdatetime"]!="" )
	{
		$sql .= " AND StartDate < '" . date('Y-m-d H:i:s', strtotime($_POST['totdatetime'])) . "'";
		#echo "3Kommando = $sql";
	}
	
	if ( isset($_POST["export"]) && $_POST["export"]=="ON" )
	{
		//Optional: print out title to top of Excel or Word file with Timestamp
		//for when file was generated:
		//set $Use_Titel = 1 to generate title, 0 not to use title
		$Use_Title = 1;
		//define date for title: EDIT this to create the time-format you need
		$now_date = date('d-m-Y H:i');
		//define title for .doc or .xls file: EDIT this if you want
		$title = "CDR Daten: $now_date";

			
		if(!$result = $db->query($sql)) { 
	 		message_die(CRITICAL_ERROR, "Could not query config information in portal_board", "", __LINE__, __FILE__, $sql);
	 		exit;
		}
		//header info for browser: determines file type ('.doc' or '.xls')
		//header ("Content-Type: application/vnd.ms-excel");
		header('Content-Type: text/csv; charset=utf-8');
		header ('Content-Disposition: attachment;filename="Export_CDR.csv"');
		header ('Cache-Control: max-age=0');
		
		/*	Start of Formatting for Word or Excel	*/

			/*	FORMATTING FOR EXCEL DOCUMENTS ('.xls')   */
			//create title with timestamp:
			if ($Use_Title == 0)
			{
				echo("$title\n");
			}
			//define separator (defines columns in excel & tabs in word)
			$sep = "\t"; //tabbed character

			//start of printing column names as names of mssql fields
			for ($i = 0; $i < $result->numColumns(); $i++)
			{
				echo $result->columnName($i) . "\t";
			}
			print("\n");
			//end of printing column names

			//start while loop to get data
			while($row = $result->fetchArray())
			{
				//set_time_limit(60); // HaRa
				$schema_insert = "";
				for($j=0; $j < $result->numColumns();$j++)

					{
					if(!isset($row[$j]))
						$schema_insert .= "NULL".$sep;
					elseif ($row[$j] != "")
						$schema_insert .= "$row[$j]".$sep;
					else
						$schema_insert .= "".$sep;
				}
				$schema_insert = str_replace($sep."$", "", $schema_insert);
				//following fix suggested by Josue (thanks, Josue!)
				//this corrects output in excel when table fields contain \n or \r
				//these two characters are now replaced with a space
				$schema_insert = preg_replace("/\r\n|\n\r|\n|\r/", " ", $schema_insert);
				$schema_insert .= "\t";
				print(trim($schema_insert));
				print "\n";
			}
	exit;
	}

	if(!$result = $db->query($sql)) { 
	 	message_die(CRITICAL_ERROR, "Could not query config information in portal_board", "", __LINE__, __FILE__, $sql);
	 	exit;
	}
	else
	{

		include('include/header.inc.php');
		echo '<br \>';
		//echo '<br \>';

		echo '<table cellpadding="0" cellspacing="0" border="0" width="98%" align="center">';
		echo ' <tr height="36">';
		echo '  <td width="26" background="images/tl-gry.gif">&nbsp;</td>';
		echo '  <td background="images/t-gry.gif" colspan="2"><b><font color=white>' . $lang['callresult'] . ':</font><b></td>';
		echo '  <td width="10" background="images/tr-gry.gif"></td>';
		echo ' </tr>';
		echo '</table>';
		
		echo '<table cellpadding="0" cellspacing="0" border="0" width="98%" align="center">';
		echo ' <tr height="10">';
		echo '  <td width="10" background="images/mt-l.gif">&nbsp;</td>';
		echo '  <td background="images/mt-c.gif" colspan="2">&nbsp;</td>';
		echo '  <td width="10" background="images/mt-r.gif">&nbsp;</td>';
		echo ' </tr>';
		echo '</table>';
		
		echo '<table cellpadding="0" cellspacing="0" border="0" width="98%" align="center">';
		echo ' <tr>';
		echo '  <td width="10" background="images/mb-l.gif">&nbsp;</td>';
 		echo '  <td align="left">&nbsp;</td>';
 		echo '  <td align="left"><b>&nbsp;' . $lang['originatingnumber'] . '</b></td>';
 		echo '  <td align="left"><b>&nbsp;' . $lang['originatingname'] . '</b></td>';
 		echo '  <td align="left"><b>' . $lang['callednumber'] . '</b></td>';
 		echo '  <td align="left"><b>&nbsp;' . $lang['calledname'] . '</b></td>';
 		echo '  <td align="left"><b>&nbsp;' . $lang['destinationnumber'] . '</b></td>';
 		echo '  <td align="left"><b>&nbsp;' . $lang['destinationname'] . '</b></td>';
 		echo '  <td align="left"><b>&nbsp;' . $lang['startime'] . '</b></td>';
 		echo '  <td align="left"><b>&nbsp;' . $lang['state'] . '</b></td>';
 		echo '  <td align="left"><b>&nbsp;' . $lang['disconnectreason'] . '</b></td>';
 		echo '  <td width="10" background="images/mb-r.gif">&nbsp;</td>';
		echo ' </tr>';

		$callduration_fromnumber = 0;
		$callduration_fromname = 0;
		$callduration_callednumber = 0;
		$callduration_calledname = 0;
		$callduration_destnumber = 0;
		$callduration_destname = 0;
		$callduration_total = 0;

		// Zähle Eintrage
		$Anzahl = 0;
		while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
			$Anzahl++;
			// TOTAL CALL DURATION
			if ( isset($_POST["fromnumber"]) && $_POST["fromnumber"]!="0" )
			{
				if ( $row['EndTime']!="" && $row['ConnectTime']!="" )
				{
					$callduration_fromnumber += (strtotime($row['EndTime'])-strtotime($row['ConnectTime']));
				}
			}

			else if ( isset($_POST["fromname"]) && $_POST["fromname"]!="0" )
			{
				if ( $row['EndTime']!="" && $row['ConnectTime']!="" )
				{
					$callduration_fromname += (strtotime($row['EndTime'])-strtotime($row['ConnectTime']));
				}
			}

			else if ( isset($_POST["callednumber"]) && $_POST["callednumber"]!="0" )
			{
				if ( $row['EndTime']!="" && $row['ConnectTime']!="" )
				{
					$callduration_callednumber += (strtotime($row['EndTime'])-strtotime($row['ConnectTime']));
				}
			}

			else if ( isset($_POST["calledname"]) && $_POST["calledname"]!="0" )
			{
				if ( $row['EndTime']!="" && $row['ConnectTime']!="" )
				{
					$callduration_calledname += (strtotime($row['EndTime'])-strtotime($row['ConnectTime']));
				}
			}

			else if ( isset($_POST["destnumber"]) && $_POST["destnumber"]!="0" )
			{
				if ( $row['EndTime']!="" && $row['ConnectTime']!="" )
				{
					$callduration_destnumber += (strtotime($row['EndTime'])-strtotime($row['ConnectTime']));
				}
			}
			
			else if ( isset($_POST["destname"]) && $_POST["destname"]!="0" )
			{
				if ( $row['EndTime']!="" && $row['ConnectTime']!="" )
				{
					$callduration_destname += (strtotime($row['EndTime'])-strtotime($row['ConnectTime']));
				}
			}

			else
			{
				if ( $row['EndTime']!="" && $row['ConnectTime']!="" )
				{
					$callduration_total += (strtotime($row['EndTime'])-strtotime($row['ConnectTime']));
				}
			}			
			# Datum umschreibenvon DB in HTML
			$Newdate = explode("-", $row['StartDate']);
			#echo '1' . $row['StartDate'] . '';
			$Newday = explode(" ", $Newdate[2]); 
			$Tag = $Newday[0];
			#echo "Tag: $Tag";
			$Monat = $Newdate[1];
			#echo "Monat: $Monat";
			$Jahr = $Newdate[0];
			#echo "Jahr: $Jahr";
			$Zeit = $Newday[1];
			#echo "Zeit: $Zeit";
			$Insertdate = "$Tag.$Monat.$Jahr $Zeit";
			
			echo '<tr id="' . $row['CallId'] . '" name="' . $row['CallId'] . '" onMouseOver="ChangeBackground(\'' . $row['CallId'] . '\')" onMouseOut="ResetBackground(\'' . $row['CallId'] . '\')">';
			echo ' <td width="10" background="images/mb-l.gif">&nbsp;</td>';
			echo ' <td align="right"><a style="cursor: pointer;" onclick="javascript:window.open(\'info.php?callid=' . $row['CallId'] . '\',\'Call_Info\',\'status=0,toolbar=0,location=0,menubar=0,scrollbars=0,width=700,height=620\');"><img src="images/info.gif" border="0" alt="' . $lang['morecallinfo'] . '" title="' . $lang['morecallinfo'] . '"></td>';
			if ( trim($row['OriginationNumber'])=="" )
			{
				echo ' <td><img src="images/nophone.gif" border="0" alt="No Number" title="No Number">&nbsp;XXX</td>';
			}
			else
			{
				echo ' <td><a href="callto://' . $row['OriginationNumber'] . '"><img src="images/phone.gif" border="0" alt="' . $lang['callthisnumber'] . '" title="' . $lang['callthisnumber'] . '"></a>&nbsp;' . $row['OriginationNumber'] . '</td>';
			}
			echo ' <td>&nbsp;' . $row['OriginationName'] . '</td>';
			echo ' <td><a href="callto://' . $row['CalledNumber'] . '"><img src="images/phone.gif" border="0" alt="' . $lang['callthisnumber'] . '" title="' . $lang['callthisnumber'] . '"></a>&nbsp;' . $row['CalledNumber'] . '</td>';
			echo ' <td>&nbsp;' . $row['CalledName'] . '</td>';
			echo ' <td><a href="callto://' . $row['CalledNumber'] . '"><img src="images/phone.gif" border="0" alt="' . $lang['callthisnumber'] . '" title="' . $lang['callthisnumber'] . '"></a>&nbsp;' . $row['DestinationNumber'] . '</td>';
			echo ' <td>&nbsp;' . $row['DestinationName'] . '</td>';
			echo " <td>&nbsp;$Insertdate</td>";
			echo ' <td>&nbsp;' . $row['State'] . '</td>';
			echo ' <td>&nbsp;' . $row['DisconnectReason'] . '</td>';
			echo ' <td width="10" background="images/mb-r.gif">&nbsp;</td>';
			echo '</tr>';
		}
		echo '</table>';

echo '<table cellpadding="0" cellspacing="0" border="0" width="98%" align="center">';
echo '<tr height="10">';
echo '  <td width="10" background="images/b-l.gif">&nbsp;</td>';
echo '  <td background="images/b-c.gif" colspan="2">&nbsp;</td>';
echo '  <td width="10" background="images/b-r.gif">&nbsp;</td>';
echo ' </tr>';
echo '</table><br>';
		
		echo '<div id="summary"><table cellspacing="0" cellpadding="0">';

		echo ' <tr height="35">';
 		echo '  <td width="26" background="images/tl-gry.gif">&nbsp;</td>';
 		echo '  <td width="485" background="images/t-gry.gif"><b><font color=white>Infos:</font><b></td>';
 		echo '  <td width="10" background="images/tr-gry.gif"></td>';
		echo ' </tr>';

		echo ' <tr background="#fbfbfb">';
 		echo '  <td colspan="3">';

		echo '<table cellspacing="0" cellpadding="0" width="100%">';

		echo ' <tr height="10">';
 		echo '  <td width="10" background="images/mt-l.gif">&nbsp;</td>';
 		echo '  <td width="501" background="images/mt-c.gif" colspan="2">&nbsp;</td>';
 		echo '  <td width="10" background="images/mt-r.gif">&nbsp;</td>';
		echo ' </tr>';

		echo ' <tr height="10">';
 		echo '  <td width="10" background="images/mb-l.gif">&nbsp;</td>';
 		echo '  <td height="22" width="250" background="#fbfbfb"><b>' . $lang['amountconversations'] . ':</b>&nbsp;</td>';
 		echo '  <td height="22" width="250" background="#fbfbfb">&nbsp;&nbsp;' . $Anzahl . '</td>';
 		echo '  <td width="10" background="images/mb-r.gif">&nbsp;</td>';
		echo ' </tr>';

		if ( isset($_POST["fromnumber"]) && $_POST["fromnumber"]!="0" )
		{

			echo ' <tr height="10">';
 			echo '  <td width="10" background="images/mb-l.gif">&nbsp;</td>';
 			echo '  <td height="22" width="250" background="#fbfbfb"><b>' . $lang['fromnumber_tot'] . ': ' . $_POST["fromnumber"] . '</b>&nbsp;</td>';
 			echo '  <td height="22" width="251" background="#fbfbfb">&nbsp;&nbsp;' . duration($callduration_fromnumber) . '</td>';
 			echo '  <td width="10" background="images/mb-r.gif">&nbsp;</td>';
			echo ' </tr>';
		}

		else if ( isset($_POST["fromname"]) && $_POST["fromname"]!="0" )
		{

			echo ' <tr height="10">';
 			echo '  <td width="10" background="images/mb-l.gif">&nbsp;</td>';
 			echo '  <td height="22" width="250" background="#fbfbfb"><b>' . $lang['fromname_tot'] . ': ' . $_POST["fromname"] . '</b>&nbsp;</td>';
 			echo '  <td height="22" width="251" background="#fbfbfb">&nbsp;&nbsp;' . duration($callduration_fromname) . '</td>';
 			echo '  <td width="10" background="images/mb-r.gif">&nbsp;</td>';
			echo ' </tr>';
		}

		else if ( isset($_POST["callednumber"]) && $_POST["callednumber"]!="0" )
		{

			echo ' <tr height="10">';
 			echo '  <td width="10" background="images/mb-l.gif">&nbsp;</td>';
 			echo '  <td height="22" width="250" background="#fbfbfb"><b>' . $lang['callednumber_tot'] . ': ' . $_POST["callednumber"] . '</b>&nbsp;</td>';
 			echo '  <td height="22" width="251" background="#fbfbfb">&nbsp;&nbsp;' . duration($callduration_callednumber) . '</td>';
 			echo '  <td width="10" background="images/mb-r.gif">&nbsp;</td>';
			echo ' </tr>';
		}

		else if ( isset($_POST["calledname"]) && $_POST["calledname"]!="0" )
		{

			echo ' <tr height="10">';
 			echo '  <td width="10" background="images/mb-l.gif">&nbsp;</td>';
 			echo '  <td height="22" width="250" background="#fbfbfb"><b>' . $lang['calledname_tot'] . ': ' . $_POST["calledname"] . '</b>&nbsp;</td>';
 			echo '  <td height="22" width="251" background="#fbfbfb">&nbsp;&nbsp;' . duration($callduration_calledname) . '</td>';
 			echo '  <td width="10" background="images/mb-r.gif">&nbsp;</td>';
			echo ' </tr>';
		}

		else if ( isset($_POST["destnumber"]) && $_POST["destnumber"]!="0" )
		{

			echo ' <tr height="10">';
 			echo '  <td width="10" background="images/mb-l.gif">&nbsp;</td>';
 			echo '  <td height="22" width="250" background="#fbfbfb"><b>' . $lang['destnumber_tot'] . ': ' . $_POST["destnumber"] . '</b>&nbsp;</td>';
 			echo '  <td height="22" width="251" background="#fbfbfb">&nbsp;&nbsp;' . duration($callduration_destnumber) . '</td>';
 			echo '  <td width="10" background="images/mb-r.gif">&nbsp;</td>';
			echo ' </tr>';			
		}

		else if ( isset($_POST["destname"]) && $_POST["destname"]!="0" )
		{

			echo ' <tr height="10">';
 			echo '  <td width="10" background="images/mb-l.gif">&nbsp;</td>';
 			echo '  <td height="22" width="250" background="#fbfbfb"><b>' . $lang['destname_tot'] . ': ' . $_POST["destname"] . '</b>&nbsp;</td>';
 			echo '  <td height="22" width="251" background="#fbfbfb">&nbsp;&nbsp;' . duration($callduration_destname) . '</td>';
 			echo '  <td width="10" background="images/mb-r.gif">&nbsp;</td>';
			echo ' </tr>';			
		}
		
		else
		{

			echo ' <tr height="10">';
 			echo '  <td width="10" background="images/mb-l.gif">&nbsp;</td>';
 			echo '  <td height="22" width="250" background="#fbfbfb"><b>' . $lang['destname_tot'] . ': ' . '</b>&nbsp;</td>';
 			echo '  <td height="22" width="251" background="#fbfbfb">&nbsp;&nbsp;' . duration($callduration_total) . '</td>';
 			echo '  <td width="10" background="images/mb-r.gif">&nbsp;</td>';
			echo ' </tr>';			
		}

		echo ' <tr height="10">';
 		echo '  <td width="10" background="images/b-l.gif">&nbsp;</td>';
 		echo '  <td width="300" background="images/b-c.gif" colspan="2">&nbsp;</td>';
 		echo '  <td width="10" background="images/b-r.gif">&nbsp;</td>';
		echo ' </tr>';

		echo '</table';

 		echo '  </td>';
		echo ' </tr>';
		
		echo '</table></div><br>';
		
	
	}

}
else
{
include('include/header.inc.php');
?>
<form id="searchconstraints" method="post" action="index.php" style="margin:0px;">
<br>
<div id="summary">
<table cellpadding="0" cellspacing="0" border="0" width="1000">
 <tr height="36">
  <td width="26" background="images/tl-gry.gif">&nbsp;</td>
  <td width="964" background="images/t-gry.gif" colspan="2"><b><font color=white><?php echo $lang['searchconstraints']; ?></font><b></td>
  <td width="10" background="images/tr-gry.gif"></td>
 </tr>
</table>
<table cellpadding="0" cellspacing="0" border="0" width="1000">
 <tr height="10">
  <td width="10" background="images/mt-l.gif">&nbsp;</td>
  <td width="980" background="images/mt-c.gif" colspan="2">&nbsp;</td>
  <td width="10" background="images/mt-r.gif">&nbsp;</td>
 </tr>
</table>
<table cellpadding="0" cellspacing="0" border="0" width="1000">
<tr>
	<td width="10" background="images/mb-l.gif">&nbsp;</td>
	<td bgcolor="fbfbfb"><?php echo $lang['searchvorthisnumber']; ?></td>
	<td bgcolor="fbfbfb"><input type="text" name="freienummer" id="freienummer" value=""></td>
	<td width="10" background="images/mb-r.gif"></td>
	
<tr>
 <td width="10" background="images/mb-l.gif">&nbsp;</td>
 <td bgcolor="fbfbfb"><?php echo $lang['fromnr']; ?></td>
 <td bgcolor="fbfbfb">
 	<select name="fromnumber" id="fromnumber" onchange="this.form.fromname.disabled=(this.value!=0);">
 	 <option value="0"><?php echo $lang['all']; ?></option>
<?php
$sql  = "SELECT OriginationNumber FROM cdr GROUP BY OriginationNumber ORDER BY OriginationNumber ASC";
if(!$result = $db->query($sql)) { 
 	message_die(CRITICAL_ERROR, "Could not query config information in portal_board", "", __LINE__, __FILE__, $sql);
 	exit;
}
else
{
	while ($row = $result->fetchArray(SQLITE3_ASSOC)) { 
  echo '<option value="' . $row['OriginationNumber'] . '">' . $row['OriginationNumber'] . '</option>';
  }
}
?> 	 
 	</select>&nbsp;
 	<select name="fromname" id="fromname" onchange="this.form.fromnumber.disabled=(this.value!=0);">
 	 <option value="0"><?php echo $lang['all']; ?></option>
<?php
$sql  = "SELECT OriginationName FROM cdr GROUP BY OriginationName ORDER BY OriginationName ASC";
if(!$result = $db->query($sql)) { 
 	message_die(CRITICAL_ERROR, "Could not query config information in portal_board", "", __LINE__, __FILE__, $sql);
 	exit;
}
else
{
	while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
		if ($row['OriginationName'] != "") {
		echo '<option value="' . $row['OriginationName'] . '">' . $row['OriginationName'] . '</option>'; }
  }
}
?> 	 
 	</select> 	
 	</td>
 	<td width="10" background="images/mb-r.gif">&nbsp;</td>
</tr>

<tr>
 <td width="10" background="images/mb-l.gif">&nbsp;</td>
 <td bgcolor="fbfbfb"><?php echo $lang['tonr']; ?></td>
 <td bgcolor="fbfbfb">
	<select name="andor"><option value="AND"><?php echo $lang['and']; ?></option><option value="OR"><?php echo $lang['or']; ?></option></select>&nbsp;
 	<select name="tonumber" id="tonumber" onchange="this.form.toname.disabled=(this.value!=0);">
 	 <option value="0"><?php echo $lang['all']; ?></option>
<?php
$sql  = "SELECT CalledNumber FROM cdr GROUP BY CalledNumber ORDER BY CalledNumber ASC";
if(!$result =$db->query($sql)) { 
 	message_die(CRITICAL_ERROR, "Could not query config information in portal_board", "", __LINE__, __FILE__, $sql);
 	exit;
}
else
{
	while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
		if ($row['CalledNumber'] != "") {
		echo '<option value="' . $row['CalledNumber'] . '">' . $row['CalledNumber'] . '</option>';}
  }
}
?> 
 	</select>&nbsp;
 	<select name="toname" id="toname" onchange="this.form.tonumber.disabled=(this.value!=0);">
 	 <option value="0"><?php echo $lang['all']; ?></option>
<?php
$sql  = "SELECT CalledName FROM cdr GROUP BY CalledName ORDER BY CalledName ASC";
if(!$result = $db->query($sql)) { 
 	message_die(CRITICAL_ERROR, "Could not query config information in portal_board", "", __LINE__, __FILE__, $sql);
 	exit;
}
else
{
	while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
		if ($row['CalledName'] != "") {
			echo '<option value="' . $row['CalledName'] . '">' . $row['CalledName'] . '</option>';
			}
	}
}
?> 
 	</select> 	
 	</td>
 	<td width="10" background="images/mb-r.gif">&nbsp;</td>
</tr>

<tr>
 <td width="10" background="images/mb-l.gif">&nbsp;</td>
 <td bgcolor="fbfbfb"><?php echo $lang['destnr']; ?></td>
 <td bgcolor="fbfbfb">
 	<select name="andor2"><option value="AND"><?php echo $lang['and']; ?></option><option value="OR"><?php echo $lang['or']; ?></option></select>&nbsp;
 	<select name="destnumber" id="destnumber" onchange="this.form.destname.disabled=(this.value!=0);">
 	 <option value="0"><?php echo $lang['all']; ?></option>
<?php
$sql  = "SELECT DestinationNumber FROM cdr GROUP BY DestinationNumber ORDER BY DestinationNumber ASC";
if(!$result = $db->query($sql)) { 
 	message_die(CRITICAL_ERROR, "Could not query config information in portal_board", "", __LINE__, __FILE__, $sql);
 	exit;
}
else
{
	while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
		if ($row['DestinationNumber'] != "") {
		echo '<option value="' . $row['DestinationNumber'] . '">' . $row['DestinationNumber'] . '</option>';}
  }
}
?>
 	</select>&nbsp;
 	<select name="destname" id="destname" onchange="this.form.destnumber.disabled=(this.value!=0);">
 	 <option value="0"><?php echo $lang['all']; ?></option>
<?php
$sql  = "SELECT DestinationName FROM cdr GROUP BY DestinationName ORDER BY DestinationName ASC";
if(!$result = $db->query($sql)) { 
 	message_die(CRITICAL_ERROR, "Could not query config information in portal_board", "", __LINE__, __FILE__, $sql);
 	exit;
}
else
{
	while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
		if ($row['DestinationName'] != "") {
		echo '<option value="' . $row['DestinationName'] . '">' . $row['DestinationName'] . '</option>';}
  }
}
?>
 	</select>
 	</td>
  <td width="10" background="images/mb-r.gif">&nbsp;</td>
</tr>

<tr>
 <td width="10" background="images/mb-l.gif">&nbsp;</td>
 <td bgcolor="fbfbfb"><?php echo $lang['conversationstate']; ?></td>
 <td bgcolor="fbfbfb">
 	<select name="conversationstate" id="conversationstate">
 	 <option value="0"><?php echo $lang['all']; ?></option>
<?php
$sql  = "SELECT State FROM cdr GROUP BY State ORDER BY State ASC";
if(!$result = $db->query($sql)) { 
 	message_die(CRITICAL_ERROR, "Could not query config information in portal_board", "", __LINE__, __FILE__, $sql);
 	exit;
}
else
{
	while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
		echo '<option value="' . $row['State'] . '">' . $row['State'] . '</option>';
  }
}
?>
 	</select>
 </td>
 <td width="10" background="images/mb-r.gif">&nbsp;</td>
</tr>
<tr>
 <td width="10" background="images/mb-l.gif">&nbsp;</td>
 <td bgcolor="fbfbfb"><?php echo $lang['disconnectreason']; ?></td>
 <td bgcolor="fbfbfb">
 	<select name="disconnectreason" id="disconnectreason">
 	 <option value="0"><?php echo $lang['all']; ?></option>
<?php
$sql  = "SELECT DisconnectReason FROM cdr GROUP BY DisconnectReason ORDER BY DisconnectReason ASC";
if(!$result = $db->query($sql)) { 
 	message_die(CRITICAL_ERROR, "Could not query config information in portal_board", "", __LINE__, __FILE__, $sql);
 	exit;
}
else
{
	while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
  echo '<option value="' . $row['DisconnectReason'] . '">' . $row['DisconnectReason'] . '</option>';
  }
}
?>
 	</select>
 </td>
 <td width="10" background="images/mb-r.gif">&nbsp;</td>
</tr>
<tr>
 <td width="10" background="images/mb-l.gif">&nbsp;</td>
 <td bgcolor="fbfbfb"><?php echo $lang['between']; ?></td>
 <td bgcolor="fbfbfb"><input type="text" name="vandatetime" id="vandatetime" value=""/>&nbsp;<img src="images/kalendar.gif" id="vankalender" style="cursor: pointer; border: 1px solid red;" title="Auswahl Datum" onmouseover="this.style.background='red';" onmouseout="this.style.background=''" />&nbsp;<input type="text" name="totdatetime" id="totdatetime" value=""/>&nbsp;<img src="images/kalendar.gif" id="totkalender" style="cursor: pointer; border: 1px solid red;" title="Auswahl Datum" onmouseover="this.style.background='red';" onmouseout="this.style.background=''" /></td>
 <td width="10" background="images/mb-r.gif">&nbsp;</td>
</tr>
<tr>
 <td width="10" background="images/mb-l.gif">&nbsp;</td>
 <td bgcolor="fbfbfb"><?php echo $lang['fromintext']; ?></td>
 <td bgcolor="fbfbfb">
 	<select name="vaninternextern" id="vaninternextern">
 	 <option value="0"><?php echo $lang['all']; ?></option>
 	 <option value="1"><?php echo $lang['from'] . ' ' . $lang['int']; ?></option>
 	 <option value="2"><?php echo $lang['from'] . ' ' . $lang['ext']; ?></option>
 	</select>
 </td>
 <td width="10" background="images/mb-r.gif">&nbsp;</td>
</tr>	
<tr>
 <td width="10" background="images/mb-l.gif">&nbsp;</td>
 <td bgcolor="fbfbfb"><?php echo $lang['tointext']; ?></td>
 <td bgcolor="fbfbfb">
 	<select name="naarinternextern" id="naarinternextern">
 	 <option value="0"><?php echo $lang['all']; ?></option>
 	 <option value="1"><?php echo $lang['to'] . ' ' . $lang['int']; ?></option>
 	 <option value="2"><?php echo $lang['to'] . ' ' . $lang['ext']; ?></option>
 	</select>
 </td>
 <td width="10" background="images/mb-r.gif">&nbsp;</td>
 </tr>
<tr>
 <td width="10" background="images/mb-l.gif">&nbsp;</td>
 <td bgcolor="fbfbfb"><?php echo $lang['importdb']; ?>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="checkbox" name="updatedb" value="ON"></td>
 <td bgcolor="fbfbfb"></td>
 <td width="10" background="images/mb-r.gif">&nbsp;</td>
</tr>
 <tr>
 <td width="10" background="images/mb-l.gif">&nbsp;</td>
 <td bgcolor="fbfbfb"><?php echo $lang['excel']; ?>&nbsp;&nbsp;<input type="checkbox" name="export" value="ON"></td>
 <td bgcolor="fbfbfb"><input type="submit" value="<?php echo $lang['search']; ?>" style="width:150px;">&nbsp;<input type="Reset" value="<?php echo $lang['reset']; ?>" style="width:150px;"></td>
 <td width="10" background="images/mb-r.gif">&nbsp;</td>
</tr>
</table>

<table cellpadding="0" cellspacing="0" border="0" width="1000">
 <tr height="10">
  <td width="10" background="images/b-l.gif">&nbsp;</td>
  <td width="980" background="images/b-c.gif" colspan="2">&nbsp;</td>
  <td width="10" background="images/b-r.gif">&nbsp;</td>
 </tr>
</table>

</div>
</form>

	<!-- Kalender scripts-->
	<script type="text/javascript">Calendar.setup({
			 	
        	inputField     :    "vandatetime",     // id of the input field
        	ifFormat       :    "%d.%m.%Y %H:%M:%S",      // format of the input field
        	button         :    "vankalender",  // trigger for the calendar (button ID)
        	align          :    "Tl",           // alignment (defaults to "Bl")
        	singleClick    :    true,
        	showsTime			 : 		true,
        	timeFormat		 :		"24"
    	});
	</script>
	<script type="text/javascript">Calendar.setup({
			 	
        	inputField     :    "totdatetime",     // id of the input field
        	ifFormat       :    "%d.%m.%Y %H:%M:%S",      // format of the input field
        	button         :    "totkalender",  // trigger for the calendar (button ID)
        	align          :    "Tl",           // alignment (defaults to "Bl")
        	singleClick    :    true,
        	showsTime			 : 		true,
        	timeFormat		 :		"24"
    	});
	</script>
	<!-- Kalender scripts--> 
<?php
}
include('include/footer.inc.php');
?>